package com.datacenter.controller;

import com.datacenter.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 数据仓库控制器
 * 
 * 业务含义：
 * 提供数据仓库的REST API接口，支持4个核心业务场景：
 * 1. 实时监控接口 - 获取资源状态、调用监控、异常预警
 * 2. 趋势分析接口 - 获取历史趋势、用户行为、热门资源
 * 3. 数据同步接口 - 手动触发增量同步
 * 4. 效率分析接口 - 获取资源活跃度、审批效率、价值评估
 */
@Slf4j
@RestController
@RequestMapping("/api/data-warehouse")
public class DataWarehouseController {
    
    @Autowired
    private RealtimeMonitorService realtimeMonitorService;
    
    @Autowired
    private TrendAnalysisService trendAnalysisService;
    
    @Autowired
    private DataSyncService dataSyncService;
    
    @Autowired
    private EfficiencyAnalysisService efficiencyAnalysisService;
    
    /**
     * 获取资源实时状态
     * 业务含义：返回当前资源的总储量、当日新增量、启用/禁用占比
     */
    @GetMapping("/monitor/resource-status")
    public Map<String, Object> getResourceRealtimeStatus() {
        log.info("获取资源实时状态");
        return realtimeMonitorService.getResourceRealtimeStatus();
    }
    
    /**
     * 获取接口调用监控数据
     * 业务含义：返回实时调用量、每分钟峰值、调用失败率
     */
    @GetMapping("/monitor/api-call")
    public Map<String, Object> getApiCallMonitorData() {
        log.info("获取接口调用监控数据");
        return realtimeMonitorService.getApiCallMonitorData();
    }
    
    /**
     * 获取异常行为预警
     * 业务含义：返回当前检测到的异常行为列表
     */
    @GetMapping("/monitor/abnormal-behaviors")
    public Map<String, Object> getAbnormalBehaviors() {
        log.info("获取异常行为预警");
        Map<String, Object> result = new java.util.HashMap<>();
        result.put("abnormalBehaviors", realtimeMonitorService.getAbnormalBehaviors());
        return result;
    }
    
    /**
     * 获取资源增长趋势分析
     * 业务含义：分析2015.05-2024.04期间资源增长趋势
     */
    @GetMapping("/trend/resource-growth")
    public Map<String, Object> getResourceGrowthTrend() {
        log.info("获取资源增长趋势分析");
        return trendAnalysisService.getResourceGrowthTrend();
    }
    
    /**
     * 获取用户行为变化分析
     * 业务含义：分析用户使用行为的变化趋势
     */
    @GetMapping("/trend/user-behavior")
    public Map<String, Object> getUserBehaviorTrend() {
        log.info("获取用户行为变化分析");
        return trendAnalysisService.getUserBehaviorTrend();
    }
    
    /**
     * 获取热门资源排行
     * 业务含义：统计9年累计下载和调用Top10资源
     */
    @GetMapping("/trend/popular-resources")
    public Map<String, Object> getPopularResourcesRanking() {
        log.info("获取热门资源排行");
        return trendAnalysisService.getPopularResourcesRanking();
    }
    
    /**
     * 手动触发增量数据同步
     * 业务含义：支持手动触发指定时间范围的增量同步
     */
    @PostMapping("/sync/incremental")
    public Map<String, Object> manualSyncIncrementalData(
            @RequestParam String startTime,
            @RequestParam String endTime) {
        log.info("手动触发增量数据同步，时间范围：{} 到 {}", startTime, endTime);
        
        LocalDateTime start = LocalDateTime.parse(startTime);
        LocalDateTime end = LocalDateTime.parse(endTime);
        
        dataSyncService.manualSyncIncrementalData(start, end);
        
        Map<String, Object> result = new java.util.HashMap<>();
        result.put("success", true);
        result.put("message", "增量数据同步任务已启动");
        result.put("startTime", startTime);
        result.put("endTime", endTime);
        return result;
    }
    
    /**
     * 获取资源活跃度分析
     * 业务含义：分析资源的使用活跃度，识别沉睡资源和高价值资源
     */
    @GetMapping("/efficiency/resource-activity")
    public Map<String, Object> getResourceActivityAnalysis() {
        log.info("获取资源活跃度分析");
        return efficiencyAnalysisService.getResourceActivityAnalysis();
    }
    
    /**
     * 获取审批效率分析
     * 业务含义：分析用户申请资源的审批效率
     */
    @GetMapping("/efficiency/approval-efficiency")
    public Map<String, Object> getApprovalEfficiencyAnalysis() {
        log.info("获取审批效率分析");
        return efficiencyAnalysisService.getApprovalEfficiencyAnalysis();
    }
    
    /**
     * 获取资源价值评估
     * 业务含义：基于使用情况评估资源价值
     */
    @GetMapping("/efficiency/resource-value")
    public Map<String, Object> getResourceValueAssessment() {
        log.info("获取资源价值评估");
        return efficiencyAnalysisService.getResourceValueAssessment();
    }
    
    /**
     * 获取数据仓库概览
     * 业务含义：返回数据仓库的整体概览信息
     */
    @GetMapping("/overview")
    public Map<String, Object> getDataWarehouseOverview() {
        log.info("获取数据仓库概览");
        
        Map<String, Object> overview = new java.util.HashMap<>();
        
        // 实时监控概览
        overview.put("realtimeStatus", realtimeMonitorService.getResourceRealtimeStatus());
        overview.put("apiCallMonitor", realtimeMonitorService.getApiCallMonitorData());
        
        // 趋势分析概览
        overview.put("resourceGrowth", trendAnalysisService.getResourceGrowthTrend());
        overview.put("popularResources", trendAnalysisService.getPopularResourcesRanking());
        
        // 效率分析概览
        overview.put("resourceActivity", efficiencyAnalysisService.getResourceActivityAnalysis());
        overview.put("approvalEfficiency", efficiencyAnalysisService.getApprovalEfficiencyAnalysis());
        
        overview.put("lastUpdateTime", LocalDateTime.now());
        
        return overview;
    }
    
    /**
     * 健康检查接口
     * 业务含义：检查数据仓库服务的健康状态
     */
    @GetMapping("/health")
    public Map<String, Object> healthCheck() {
        Map<String, Object> health = new java.util.HashMap<>();
        health.put("status", "UP");
        health.put("timestamp", LocalDateTime.now());
        health.put("version", "1.0.0");
        health.put("description", "数据仓库学习项目运行正常");
        return health;
    }
}
